﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Reflection;
using System.ComponentModel;
using System.Windows.Forms;
using ImageEffect.Effects;

namespace ImageEffect
{
    public class EffectsCollection : System.Collections.ObjectModel.Collection<Effect>, ICustomTypeDescriptor
    {
        // Implementation of ICustomTypeDescriptor: 
        public String GetClassName()
        {
            return TypeDescriptor.GetClassName(this, true);
        }

        public AttributeCollection GetAttributes()
        {
            return TypeDescriptor.GetAttributes(this, true);
        }

        public String GetComponentName()
        {
            return TypeDescriptor.GetComponentName(this, true);
        }

        public TypeConverter GetConverter()
        {
            return TypeDescriptor.GetConverter(this, true);
        }

        public EventDescriptor GetDefaultEvent()
        {
            return TypeDescriptor.GetDefaultEvent(this, true);
        }

        public PropertyDescriptor GetDefaultProperty()
        {
            return TypeDescriptor.GetDefaultProperty(this, true);
        }

        public object GetEditor(Type editorBaseType)
        {
            return TypeDescriptor.GetEditor(this, editorBaseType, true);
        }

        public EventDescriptorCollection GetEvents(Attribute[] attributes)
        {
            return TypeDescriptor.GetEvents(this, attributes, true);
        }

        public EventDescriptorCollection GetEvents()
        {
            return TypeDescriptor.GetEvents(this, true);
        }

        public object GetPropertyOwner(PropertyDescriptor pd)
        {
            return this;
        }

        public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            return GetProperties();
        }

        public PropertyDescriptorCollection GetProperties()
        {
            // Create a new collection object PropertyDescriptorCollection
            PropertyDescriptorCollection pColor = new PropertyDescriptorCollection(null);

            // Iterate the list of effects
            for (int i = 0; i < this.Count; i++)
            {
                // For each employee create a property descriptor 
                // and add it to the 
                // PropertyDescriptorCollection instance
                EffectCollectionPropertyDescriptor pd = new EffectCollectionPropertyDescriptor(this, i);

                pColor.Add(pd);
            }

            return pColor;
        }
    }

    public class EffectCollectionPropertyDescriptor : PropertyDescriptor
    {
        private EffectsCollection collection = null;
        Effect effect = null;

        public EffectCollectionPropertyDescriptor(EffectsCollection coll,
                           int idx)
            : base(coll[idx].GetHashCode().ToString(), null)
        {
            this.collection = coll;
            this.effect = coll[idx];
        }

        public override AttributeCollection Attributes
        {
            get
            {
                return TypeDescriptor.GetAttributes(this);
            }
        }

        public override bool CanResetValue(object component)
        {
            return true;
        }

        public override Type ComponentType
        {
            get
            {
                return this.collection.GetType();
            }
        }

        public override string DisplayName
        {
            get
            {
                return effect.GetName();
            }
        }

        public override string Description
        {
            get
            {
                return this.effect.GetType().ToString();
            }
        }

        public override object GetValue(object component)
        {
            return this.effect;
        }

        public override bool IsReadOnly
        {
            get { return true; }
        }

        public override string Name
        {
            get { return this.effect.GetHashCode().ToString(); }
        }

        public override Type PropertyType
        {
            get { return this.effect.GetType(); }
        }

        public override void ResetValue(object component) { }

        public override bool ShouldSerializeValue(object component)
        {
            return true;
        }

        public override void SetValue(object component, object value)
        {
            MessageBox.Show("Setting Value!\n");
            if (component.GetType() == typeof(System.Drawing.Point))
            {
            }
            // this.collection[index] = value;
        }
    }
}
